#include "BackGround.h"
#include "Global.h"
#include "Directx.h"
#include <string>

BackGround::BackGround(void){
}

BackGround::~BackGround(void){}

//BackGround::BackGround(int left, int top,int level,int t)
//{
//	char path[20];
//	sprintf_s(path, "map\\%d\\%d.bmp", level,t);
//	texture = Global::_Directx->LoadTextureFromFile(path,0);
//	sprite = new Sprite(texture,TILE_SIZE,TILE_SIZE,1,1);
//	id = t;
//	UpdateRect(left,top);
//}
//void BackGround::RenderBG()
//{
//	list<Static*>::iterator it;
//	for ( it = Global::listBG.begin(); it!=Global::listBG.end(); it++)
//	{
//		(*it)->Render();
//	}
//}
bool BackGround::initialize(int level)
{
	char path[20];
	for (int i = 0; i < Global::rows; i++)
	{
		vector<Sprite*> _Rows;
		for (int j = 0; j < Global::cols; j++)
		{
			sprintf_s(path, "map\\%d\\%d.bmp", Global::maplevel, Global::mtbg[i][j]);
			_Rows.push_back(InitObject(path));
		}
		backgroundsprite.push_back(_Rows);
	}
	return 1;
}
//
//void BackGround::loadmatrix(char* map)
//{
//	vector<string> temp;
//	temp = readfiletxt(map);
//	rows = atoi(temp.at(0).c_str());
//	cols = atoi(temp.at(1).c_str());
//	//MessageBoxW(0,L"test",L"ok",MB_OK);
//	mtbg[rows][cols];
//	int k = 2;
//	for (int i = 0; i < rows; i ++)
//	{
//		for (int j = 0; j < cols; j ++)
//		{
//			mtbg[i][j] = atoi(temp.at(k).c_str());
//			k++;
//		}
//	}
//}
//
Sprite* BackGround::InitObject(char *tilepath)
{
	texture = Global::_Directx->LoadTextureFromFile(tilepath,0);
	Sprite* _Sprite = new Sprite(texture, TILE_SIZE, TILE_SIZE, 1, 1);
	return _Sprite;
}
void BackGround::RenderMap(int left, int right)
{
	RECT screen = Global::Screen;
	int startx = left/TILE_SIZE;
	int endx = right/TILE_SIZE;
	for (int i = 0; i < Global::rows; i ++)
	{
		for (int j = startx; j <= endx; j ++)
		{
			backgroundsprite.at(i).at(j)->Render(backbuffer, j*TILE_SIZE, (Global::rows - i)*TILE_SIZE,screen.left,screen.bottom);
		}
	}
	
	
}